package com.cjb.eighteenart.util;

import com.cjb.common.util.Common;
import com.zjiecode.wxpusher.client.WxPusher;
import com.zjiecode.wxpusher.client.bean.Message;
import com.zjiecode.wxpusher.client.bean.MessageResult;
import com.zjiecode.wxpusher.client.bean.Result;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class MsgAPIUtil
 * @Date 2024-01-30 0:59
 */
@Service
public class MsgAPIUtil {
    /**
     * 日志
     */
    private final static Logger log = LoggerFactory.getLogger(MsgAPIUtil.class);

    /*注入系统变量*/
    @Value("${spring.mail.username}")
    private String sendQQMail;
    /*注入wxpusher信息*/
    @Value("${wxpusher.topicId}")
    private String topicId;
    @Value("${wxpusher.appToken}")
    private String appToken;
    @Value("${wxpusher.url}")
    private String url;

    /**
     * 注入邮箱发送服务
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Autowired
    private JavaMailSenderImpl mailSender;

    /**
     * 发送邮箱api
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    public void contextLoads(String theme,String receiveString,String receiveQQMail) throws MessagingException {
        log.info("打印配置信息 {}",sendQQMail);
        sendWxPusher(theme,receiveString,receiveQQMail);
    }

    /**
     *
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    /**
     * 信息发送接口
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    public String searchGoods(String goodsName, int page, int pageSize) {
        String baseUrl = "https://h5.baoge.art";
        String url = baseUrl + "/api/v1/search/goods-search?isSearch=true&page=" + page + "&pageSize=" + pageSize + "&goodsName=" + goodsName;

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> entity = new HttpEntity<>(headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);

        if (response.getStatusCode() == HttpStatus.OK) {
//            log.info("打印回传的消息 {}", JsonUtils.toJsonString(response.getBody()));
            return response.getBody();
        } else {
            throw new RuntimeException("Failed to fetch data from the API: " + response.getStatusCode());
        }
    }


    /**
     * 发送wxpusher
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    private void sendWxPusher(String theme, String receiveString,String uid) {
        // 发送消息
        try {
            Message message = new Message();
            message.setTopicId(27442L);
            if (Common.isNotEmpty(uid)){
                message.setUid(uid);
            }
            message.setAppToken("AT_9GPt9gfeqysslP3KBuMfGxaNLt3aHpQF");
            message.setContentType(Message.CONTENT_TYPE_TEXT);
            message.setContent("您好！您订阅的"+theme+receiveString+"！");
            message.setUrl("http://wxpuser.zjiecode.com");//可选参数
            Result<List<MessageResult>> result = WxPusher.send(message);
            System.out.println(result.isSuccess());
        } catch (Exception e) {
            log.error("发送微信推送失败", e);
        }
    }
}
